﻿using System;
using System.Collections.Generic;
using System.Text;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.IBatisRepository;
using OneMG.Phoenix.Respository;
using System.Data;
using OneMG.Phoenix.Entity;

namespace OneMG.Phoenix.Service
{
    public class OrganizationService : IOrganizationService
    {
		 IOrganization organization = new OrganizationRepository();
		 

		public void UpdateByOrgID (OrganizationModel model)
		{
			EOrganization entity = ConvertModelToEntity(model);

            organization.UpdateByOrgID(entity);

		}

		public void DeleteByOrgID (System.Int32 orgID)
		{
		
            organization.DeleteByOrgID(orgID);

		}
		 
        public OrganizationModel GetByOrgID (System.Int32 orgID)
        {
        
			EOrganization entity = organization.GetByOrgID(orgID);
			
            OrganizationModel model = ConvertEntityToModel(entity);


            return model;
        }

        public IList<OrganizationModel> GetByParentID(int parentID)
        {
            IList<EOrganization> listEntity = organization.GetByParentID(parentID);

            if (listEntity == null)
                return null;

            IList<OrganizationModel> listModel = new List<OrganizationModel>();

            foreach (var item in listEntity)
            {
                OrganizationModel model = ConvertEntityToModel(item);

                listModel.Add(model);
            }
            return listModel;



        }

        public IList<OrganizationModel> GetByParentIDPagedList(int parentID, int pageSize, int pageIndex, out int totalRecord)
        {

            IList<EOrganization> listEntity = organization.GetByParentIDPagedList(parentID,pageSize,pageIndex,out totalRecord);

            if (listEntity == null)
                return null;

            IList<OrganizationModel> listModel = new List<OrganizationModel>();

            foreach (var item in listEntity)
            {
                OrganizationModel model = ConvertEntityToModel(item);

                listModel.Add(model);
            }
            return listModel;
        }

        
		public OrganizationModel ConvertEntityToModel(EOrganization entity)
		{
			if (entity == null)
            {
                return null;
            }
			OrganizationModel model = new OrganizationModel();
                        model.OrgID = entity.OrgID;
                        model.OrgName = entity.OrgName;
                        model.OrgType = entity.OrgType;
                        model.ParentID = entity.ParentID;
                        model.IDPath = entity.IDPath;
                        model.NamePath = entity.NamePath;
                        model.LeaderID = entity.LeaderID;
                        model.LeaderName = entity.LeaderName;
                        model.OfficePhone = entity.OfficePhone;
                        model.Address = entity.Address;
                        model.DeleteFlag = entity.DeleteFlag;
                        model.DeleteTime = entity.DeleteTime;
                        model.DeleteUser = entity.DeleteUser;
                        model.CreateTime = entity.CreateTime;
                        model.CreateUser = entity.CreateUser;
                        model.UpdateTime = entity.UpdateTime;
                        model.UpdateUser = entity.UpdateUser;
                        return model;
		}
		
		public EOrganization ConvertModelToEntity(OrganizationModel model)
		{
			if (model == null)
            {
                return null;
            }
			EOrganization entity = new EOrganization();
                        entity.OrgID = model.OrgID;
                        entity.OrgName = model.OrgName;
                        entity.OrgType = model.OrgType;
                        entity.ParentID = model.ParentID;
                        entity.IDPath = model.IDPath;
                        entity.NamePath = model.NamePath;
                        entity.LeaderID = model.LeaderID;
                        entity.LeaderName = model.LeaderName;
                        entity.OfficePhone = model.OfficePhone;
                        entity.Address = model.Address;
                        entity.DeleteFlag = model.DeleteFlag;
                        entity.DeleteTime = model.DeleteTime;
                        entity.DeleteUser = model.DeleteUser;
                        entity.CreateTime = model.CreateTime;
                        entity.CreateUser = model.CreateUser;
                        entity.UpdateTime = model.UpdateTime;
                        entity.UpdateUser = model.UpdateUser;
                        return entity;
		}


    }
}